<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_DataBinder
 * @subpackage BindingRules
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * Wrapper for array of binding rules.
 *
 * @package SmartL_DataBinder
 * @subpackage BindingRules
 */
class SmartL_DataBinder_BindingRules implements Iterator, Countable {
	/**
	 * @var array[numeric]=>SmartL_DataBinder_BindingRule
	 */
	private $_rules = array ();
	
	/**
	 * @param array $rules
	 */
	public function __construct($rules) {
		$this->makeRules ( $rules );
	}
	
	/**
	 * Makes SmartL_DataBinder_BindingRule objects instances from given array.
	 *
	 * @param array $data
	 */
	private function makeRules($data) {
		foreach ( $data as $key => $value ) {
			if ($value instanceof SmartL_DataBinder_BindingRule) {
				$this->_rules [] = $value;
			} else {
				$first = is_string ( $key ) ? $key : $value;
				$this->_rules [] = new SmartL_DataBinder_BindingRule ( $first, $value );
			}
		}
	}
	
	public function switchRules() {
		foreach ( $this->_rules as $rule ) {
			$rule->switchRule ();
		}
	}
	
	//------------------------------------//
	//Implemented from interfaces 
	

	/**
	 * Implementation of Countable inteface.
	 * @return integer Count of binding rules.
	 */
	public function count() {
		return count ( $this->_rules );
	}
	
	/**
	 * Implementation of Iterator interface.
	 * Resets internal pointer to first binding rule.
	 */
	public function rewind() {
		reset ( $this->_rules );
	}
	
	/**
	 * Implementation of Iterator interface.
	 * Moves internal pointer to next child.
	 * @return SmartL_DataBinder_BindingRule The next binding rule.
	 */
	public function next() {
		return next ( $this->_rules );
	}
	
	/**
	 * Implementation of Iterator interface.
	 * @return SmartL_DataBinder_BindingRule Current binding rule.
	 */
	public function current() {
		return current ( $this->_rules );
	}
	
	/**
	 * Implementation of Iterator interface.
	 * @return integer Key of current binding rule.
	 */
	public function key() {
		return key ( $this->_rules );
	}
	
	/**
	 * Implementation of Iterator interface.
	 * @return boolean True, if the internal pointer is on valid position.
	 */
	public function valid() {
		return current ( $this->_rules ) !== false;
	}
}
